package
{	
	import flash.display.Stage;
	import flash.events.AccelerometerEvent;
	import flash.events.MouseEvent;
	import flash.sensors.Accelerometer;
	
	import nape.callbacks.CbType;
	import nape.constraint.PivotJoint;
	import nape.geom.Vec2;
	import nape.phys.Body;
	import nape.phys.BodyList;
	import nape.phys.BodyType;
	import nape.phys.Material;
	import nape.shape.Circle;
	import nape.shape.Polygon;
	import nape.space.Space;
	
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	public class Main extends Sprite
	{	
		[Embed(source="back.png")]
		private var back:Class;
		
		private var space:Space;
		private var box:Body;
		private var ship:Image;
		private var ball:Body;
		private var hand:PivotJoint;
		private var tx:Number;
		private var ty:Number;
		private var touch:Touch;
		private var ns:Stage;
		
		public function Main()
		{	
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(event:Event):void
		{
			addChild(Image.fromBitmap(new back()));
			
			ns = Starling.current.nativeStage;
			
			space = new Space(new Vec2(0, 3000));
			
			var floor:Body = new Body(BodyType.STATIC); 
			floor.shapes.add(new Polygon(Polygon.rect(0, 768, 1024, 20)));
			floor.shapes.add(new Polygon(Polygon.rect(1024, 0, 200, 768)));
			floor.shapes.add(new Polygon(Polygon.rect(0, -20, 1024, 20)));
			floor.shapes.add(new Polygon(Polygon.rect(-200, 0, 200, 768)));
			floor.space = space;
			
			hand = new PivotJoint(space.world,null,new Vec2(),new Vec2());
			hand.active = false;
			hand.stiff = false;
			hand.space = space;
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			ns.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			ns.addEventListener(MouseEvent.MOUSE_UP, onUp);
			
			var acc:Accelerometer = new Accelerometer();
			acc.addEventListener(AccelerometerEvent.UPDATE, onUpdate);
			for(var i:int=0; i<20; i++) addBall();
		}
		
		protected function onUpdate(event:AccelerometerEvent):void
		{
			space.gravity = new Vec2(-event.accelerationX*5000, 3000);
		}
		
		protected function onUp(event:MouseEvent):void
		{
			hand.active = false;
		}
		
		protected function onDown(event:MouseEvent):void
		{
			var mp:Vec2 = new Vec2(event.stageX, event.stageY);
			var bodies:BodyList = space.bodiesUnderPoint(mp);
			if(bodies.length > 0)
			{
				var b:Body = bodies.shift();
				hand.body2 = b;
				hand.anchor2 = b.worldToLocal(mp);
				hand.active = true;
			}
		}
		
		private function onEnterFrame(event:Event):void
		{
			hand.anchor1.setxy(ns.mouseX, ns.mouseY);
			space.step(1/60);			
		}
		
		private function addBall():void
		{
			ball = new Body(BodyType.DYNAMIC, new Vec2((Math.random() * 750 + 50), 100));
			
			ball.shapes.add((new Circle(51.5, null, new Material(1.5))));
			ball.space = space;		
			ball.graphic = new Basketball();		
			ball.graphicUpdate = updateGraphics;
			addChild(ball.graphic);
		}
		
		private function updateGraphics(b:Body):void
		{
			b.graphic.x = b.position.x;
			b.graphic.y = b.position.y;
			b.graphic.rotation = b.rotation;
		}
	}
}